import time

from interview_voice_project.__001__langgraph_more_node import AgentState
from interview_voice_project.__003__fastapi import update_mysql

from interview_voice_project.common import get_project_root, config_ini
from pydub import AudioSegment
import os
import shutil


async def split_audio(state: AgentState, file_path, output_root="output", segment_length=60000, overlap=2000):
    """
    切割音频文件并返回生成的片段文件路径列表

    :param file_path: 输入的音频文件路径
    :param output_root: 输出的根目录（默认 "output"）
    :param segment_length: 每段音频的时长（毫秒）
    :param overlap: 重叠时长（毫秒）
    :return: 切割后生成的文件路径列表
    """
    await update_mysql("开始切分语音", record_id=state["record_id"])

    # 提取文件名（不带路径和扩展名）
    base_name = os.path.splitext(os.path.basename(file_path))[0]

    # 创建输出目录（年月日_时分秒_文件名）
    output_dir = os.path.join(output_root, f"{base_name}")

    # 如果目录已存在 → 清空目录
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)  # 删除整个目录
    os.makedirs(output_dir, exist_ok=True)

    # 加载音频
    audio = AudioSegment.from_file(file_path)
    audio_length = len(audio)

    output_files = []  # 存储生成的文件路径

    # 切割逻辑
    start = 0
    counter = 1
    while start < audio_length:
        end = min(start + segment_length, audio_length)
        segment = audio[start:end]

        # 文件名：年月日_时分秒_文件名_segment_001.wav
        out_file = os.path.join(
            output_dir,
            f"{base_name}_segment_{counter:03d}.wav"
        )
        segment.export(out_file, format="wav")
        print(f"导出: {out_file}")

        output_files.append(out_file)

        # 更新下一个开始点（注意重叠部分）
        start = start + segment_length - overlap
        counter += 1

    await update_mysql("完成切分语音", record_id=state["record_id"])
    return output_files, audio_length


async def split_voice_node(state: AgentState):
    input_audio_path = state['input_audio_path']
    # 获取voice路径
    out_file_path = get_project_root.joinpath("voice")

    _segment_length = int((config_ini.VOICE_MODEL.SEGMENT_LENGTH_MS or "60000").strip())
    _overlap = int((config_ini.VOICE_MODEL.OVERLAP_MS or "2000").strip())

    split_audio_path_list, audio_length = await split_audio(state, input_audio_path, out_file_path, _segment_length, _overlap)
    total_sec = int(audio_length // 1000)
    tips = f"开始切分语音（总时长 {total_sec//60}分{total_sec%60}秒，预计分段 {max(1, (audio_length + _segment_length - 1)//_segment_length)} 块）"
    await update_mysql(tips, record_id=state["record_id"]) 
    await update_mysql(f"语音共切割了{len(split_audio_path_list)}块", record_id=state["record_id"])
    state['split_audio_path_list'] = split_audio_path_list
    return state


if __name__ == '__main__':
    split_voice_node(
        {"input_audio_path": "/Users/duyi/PycharmProjects/interview_voice_project/__001__data/罗培鑫面试.aac"})
